/**
 * $version:  0.1 
 * $Date: 2011-01-21 
 *
 * Copyright (C) 2010-2011 Jawa Software. All rights reserved.
 *
 */

package org.jawa.learning.webapp.util;

import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

import org.jawa.core.JawaServer;
import org.jawa.core.cache.Cache;
import org.jawa.core.cache.CacheFactory;
import org.jawa.core.util.JawaGlobals;
import org.jawa.learning.model.User;

/**
 * 页面管理工具类.
 */
public class WebManager extends WebBean {

	private int range = 15;

	public WebManager() {
	}

	/**
	 * Returns the XMPP server object -- can get many config items from here.
	 */
	public JawaServer getJawaServer() {
		final JawaServer jawaServer = JawaServer.getInstance();
		if (jawaServer == null) {
			// Show that the server is down
			return null;
		}
		return jawaServer;
	}

	/**
	 * Returns the name of the application.
	 * 
	 * @return the name of the application.
	 */
	public static String getAppName() {
		String appName = JawaGlobals.getXMLProperty("global.appName");
		if (appName != null) {
			return appName;
		}
		return null;
	}

	/**
	 * Returns the URL of the main logo image for the admin console.
	 * 
	 * @return the logo image.
	 */
	public static String getLogoImage() {
		String globalLogoImage = JawaGlobals.getXMLProperty("global.logoImage");
		if (globalLogoImage != null) {
			return globalLogoImage;
		}
		return null;
	}

	/**
	 * Returns the URL of the login image for the admin console.
	 * 
	 * @return the login image.
	 */
	public static String getLoginLogoImage() {
		String globalLogoImage = JawaGlobals.getXMLProperty("global.loginImage");
		if (globalLogoImage != null) {
			return globalLogoImage;
		}
		return null;
	}

	/**
	 * 可能有问题，请参考原来实现
	 * @return the version string.
	 */
	public static String getVersionString() {
		return JawaServer.getInstance().getVersion().getVersionString();
	}

	/**
	 * Returns <tt>true</tt> if the Openfire container is in setup mode,
	 * <tt>false</tt> otherwise.
	 */
	public boolean isSetupMode() {
		return getJawaServer().isSetupMode();
	}

	public boolean isServerRunning() {
		return !(getJawaServer() == null);
	}


	/**
	 * 返回当前页面的用户.
	 */
	public User getUser() {
		User pageUser = null;
//		try {
//			pageUser = UserManager.getInstance().getUserByUsername(getAuthToken().getUsername());
//		} catch (Exception ignored) {
//			// 忽略.
//		}
		return pageUser;
	}

	/**
	 * 记录当前登录用户的安全事件,用于审计。
	 *
	 * @param summary 事件标题.
	 * @param details 事件详情.
	 */
	public void logEvent(String summary, String details) {
		// SecurityAuditManager.getInstance().logEvent(getUser().getUsername(), summary, details);
	}



	/**
	 * 返回分页情况下的当前页码(目前适用于使用DisplayTag的情况).
	 * 
	 * @return 当前页码
	 */
	public int getPageIndex(int pageRange,String pageId) {
		int pageIndex = 0;
		String pageNumberName = new org.displaytag.util.ParamEncoder(pageId)
				.encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE); // 页数的参数名
		int pageNumber = org.apache.commons.lang.StringUtils.isEmpty(request.getParameter(pageNumberName)) ? 1
				: (Integer.parseInt(request.getParameter(pageNumberName))); // 当前页数

		pageIndex = (pageNumber - 1) * pageRange;

		return pageIndex;
	}

	private void sleep() {
		try {
			Thread.sleep(3000L);
		} catch (Exception ignored) {
			// 忽略.
		}
	}

	public Cache[] getCaches() {
		Cache[] caches = CacheFactory.getAllCaches();
		Arrays.sort(caches, new Comparator<Cache>() {
			public int compare(Cache cache1, Cache cache2) {
				return cache1.getName().toLowerCase().compareTo(cache2.getName().toLowerCase());
			}
		});
		return caches;
	}

	public WebManager getManager() {
		return this;
	}

}